﻿//----------------------------------------------------------------
// <copyright file="TeamServerSection.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace MptCodeLibrary.Configuration
{
    public class TeamServerConfiguration : ConfigurationElement
    {
        /// <summary>
        /// Gets or sets the TFS server name
        /// </summary>
        [ConfigurationProperty("servername", IsKey = true, IsRequired = true)]
        public string ServerName
        {
            get { return (string)this["servername"]; }
            set { this["servername"] = value; }
        }

        /// <summary>
        /// Gets or sets the date when the last rollup was process for this server.
        /// </summary>
        [ConfigurationProperty("rollupupdated", IsKey = false, IsRequired = false)]
        public string RollupLastUpdated
        {
            get { return (string)this["rollupupdated"]; }
            set { this["rollupupdated"] = value; }           
        }

        public override bool IsReadOnly()
        {
            return false;
        }
    }

    public class TeamServerSection : ConfigurationSection, ITeamServerConfigurationList
    {
        const string TeamServersProperty = "teamservers";

        /// <summary>
        /// Gets/sets the list of team servers in the config file section.
        /// </summary>
        /// <value>The team server list.</value>
        [ConfigurationCollection(typeof(TeamServerConfiguration), AddItemName = "teamserver", CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
        [ConfigurationProperty(TeamServersProperty)]
        public virtual TeamServerConfigurationCollection TeamServers
        {
            get { return (TeamServerConfigurationCollection)base[TeamServersProperty]; }
        }

        /// <summary>
        /// Gets an enumerator for the list of the team servers from the config file section
        /// </summary>
        /// <returns>An enumerator</returns>
        public IEnumerable<TeamServerConfiguration> GetTeamServers()
        {
            return TeamServers;
        }
    }

    interface ITeamServerConfigurationList
    {
        /// <summary>
        /// Gets an enumerator for the list of the team servers from the config file section
        /// </summary>
        /// <returns>An enumerator</returns>
        IEnumerable<TeamServerConfiguration> GetTeamServers();
    }

    /// <summary>
    /// Collection of Web.config rows, in TeamServerSubSection. 
    /// </summary>
    public class TeamServerConfigurationCollection : ConfigurationElementCollection, IEnumerable<TeamServerConfiguration>
    {
        ///<summary>
        ///Returns an enumerator that iterates through the collection of TeamServerConfiguration.
        ///</summary>
        ///<returns>
        ///A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        ///</returns>
        ///<filterpriority>1</filterpriority>
        public virtual new IEnumerator<TeamServerConfiguration> GetEnumerator()
        {
            IEnumerator enumerator = base.GetEnumerator();
            while (enumerator.MoveNext())
            {
                yield return (TeamServerConfiguration)enumerator.Current;
            }
        }

        ///<summary>
        ///Creates a new <see cref="T:System.Configuration.ConfigurationElement"></see>.
        ///</summary>
        ///<returns>
        ///A new <see cref="T:System.Configuration.ConfigurationElement"></see> of derived type TeamServerConfiguration.
        ///</returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new TeamServerConfiguration();
        }

        ///<summary>
        ///Gets the element key, ServerName, for a TeamServerSubSection row configuration element.
        ///</summary>
        ///<returns>
        ///An <see cref="T:System.Object"></see> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"></see>.
        ///</returns>
        ///<param name="element">The <see cref="T:System.Configuration.ConfigurationElement"></see> to return the key for. </param>
        protected override object GetElementKey(ConfigurationElement element)
        {
            TeamServerConfiguration teamServerConfiguration = (TeamServerConfiguration)element;

            return teamServerConfiguration.ServerName;
        }
    }
}
